package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.Assets;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.THouseAssets;
import com.ruoyi.system.domain.TLandAssets;
import com.ruoyi.system.mapper.AssetsMapper;
import com.ruoyi.system.mapper.THouseAssetsMapper;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TLeaseManageMapper;
import com.ruoyi.system.domain.TLeaseManage;
import com.ruoyi.system.service.ITLeaseManageService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 租赁管理基本信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-18
 */
@Service("TLeaseManageService")
public class TLeaseManageServiceImpl implements ITLeaseManageService 
{
    @Autowired
    private TLeaseManageMapper tLeaseManageMapper;
    @Autowired
    private AssetsMapper assetsMapper;
    @Autowired
    private THouseAssetsMapper houseAssetsMapper;

    /**
     * 查询租赁管理基本信息
     * 
     * @param id 租赁管理基本信息主键
     * @return 租赁管理基本信息
     */
    @Override
    public TLeaseManage selectTLeaseManageById(Long id)
    {
        return tLeaseManageMapper.selectTLeaseManageById(id);
    }

    /**
     * 查询租赁管理基本信息列表
     * 
     * @param tLeaseManage 租赁管理基本信息
     * @return 租赁管理基本信息
     */
    @Override
    public List<TLeaseManage> selectTLeaseManageList(TLeaseManage tLeaseManage)
    {
        return tLeaseManageMapper.selectTLeaseManageList(tLeaseManage);
    }

    /**
     * 新增租赁管理基本信息
     * 
     * @param tLeaseManage 租赁管理基本信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTLeaseManage(TLeaseManage tLeaseManage)
    {
        tLeaseManage.setCreateTime(DateUtils.getNowDate());
        int i = tLeaseManageMapper.insertTLeaseManage(tLeaseManage);
        if (i >0 ){//新增成功 处理统计数据
            BigDecimal acreage = tLeaseManage.getAcreage();
            Long type = tLeaseManage.getType();
            Assets assets = assetsMapper.selectDeptById(type);
            if (assets.getRentalArea() != null){
                assets.setRentalArea(acreage.add(assets.getRentalArea()));
            }else {
                assets.setRentalArea(acreage);
            }
            int i1 = assetsMapper.updateDept(assets);
        }
        return i;
    }

    /**
     * 修改租赁管理基本信息
     * 
     * @param tLeaseManage 租赁管理基本信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTLeaseManage(TLeaseManage tLeaseManage)
    {
        tLeaseManage.setUpdateTime(DateUtils.getNowDate());
        //关联修改
        TLeaseManage leaseManage = tLeaseManageMapper.selectTLeaseManageById(Long.valueOf(tLeaseManage.getId()));
        BigDecimal acreage = leaseManage.getAcreage();//修改前
        BigDecimal acreage1 = tLeaseManage.getAcreage();//修改后
        BigDecimal subtract = acreage1.subtract(acreage);
        THouseAssets tHouseAssets = houseAssetsMapper.selectTHouseAssetsById(Long.valueOf(tLeaseManage.getAssetsId()));
        Assets assets = assetsMapper.selectDeptById(tHouseAssets.getType());
        if (assets.getRentalArea() != null){
            assets.setRentalArea(assets.getRentalArea().add(subtract));
        }else {
            assets.setRentalArea(acreage1);
        }
        assetsMapper.updateDept(assets);
        int i = tLeaseManageMapper.updateTLeaseManage(tLeaseManage);
        return i;
//        return tLeaseManageMapper.updateTLeaseManage(tLeaseManage);
    }

    /**
     * 批量删除租赁管理基本信息
     * 
     * @param ids 需要删除的租赁管理基本信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTLeaseManageByIds(String ids)
    {
        //逻辑删除
        String[] split = ids.split(",");
        int i = 0;
        for ( String s :split){
            TLeaseManage leaseManage = tLeaseManageMapper.selectTLeaseManageById(Long.valueOf(s));
            THouseAssets tHouseAssets = houseAssetsMapper.selectTHouseAssetsById(Long.valueOf(leaseManage.getAssetsId()));
            Assets assets = assetsMapper.selectDeptById(tHouseAssets.getType());
            if (assets.getRentalArea() != null){
                assets.setRentalArea(assets.getRentalArea().subtract(leaseManage.getAcreage()));
            }
            assetsMapper.updateDept(assets);
            leaseManage.setDelFlag(1);
            int i1 = tLeaseManageMapper.updateTLeaseManage(leaseManage);
            if (i1 > 0 ){
                i++;
            }
        }
        return i;
//        return tLeaseManageMapper.deleteTLeaseManageByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除租赁管理基本信息信息
     * 
     * @param id 租赁管理基本信息主键
     * @return 结果
     */
    @Override
    public int deleteTLeaseManageById(Long id)
    {
        return tLeaseManageMapper.deleteTLeaseManageById(id);
    }

    @Override
    public List<TLeaseManage> selectTLeaseManageListByTypeId(TLeaseManage tLeaseManage) {
        return tLeaseManageMapper.selectTLeaseManageListByTypeId(tLeaseManage);
    }

    @Override
    public TLeaseManage selectTLeaseManageByTime(String date) {
        return tLeaseManageMapper.selectTLeaseManageByTime(date);
    }
}
